﻿using System;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using TidyBlog.Util;
using TidyBlog.MVC;
using TidyBlog.Entity;
using TidyBlog.BLL;

namespace TidyBlog.MVC.WWW.Controllers
{
    /// <summary>
    /// 用户控制器 /User/{action}
    /// </summary>
    public class UserController : FrontController
    {
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login(string redirect)
        {
            ViewBag.Redirect = String.IsNullOrEmpty(redirect) ? Request.UrlReferrer.ToString() : redirect;

            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="captcha">验证码</param>
        /// <param name="isRemember">是否记住密码</param>
        /// <param name="redirect">重定向地址</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string userName, string password, string captcha, string isRemember, string redirect)
        {
            if (!CaptchaManager.Check("login", captcha))
            {
                throw new Exception("验证码错误");
            }

            User user = null;
            if (!String.IsNullOrEmpty(password))
            {
                password = Encrypt.ToSHA1(password);
            }
            user = UserBLL.Read(userName, password);

            if (user == null)
            {
                throw new Exception("用户不存在或密码错误");
            }

            // 写入Cookie
            HttpCookie userNameCookie = new HttpCookie("UserName"), passwordCookie = new HttpCookie("Password");

            DateTime expires = DateTime.UtcNow;
            expires = String.IsNullOrEmpty(isRemember) ? DateTime.UtcNow.AddMinutes(20) : expires.AddYears(1);

            userNameCookie.Expires = expires;
            passwordCookie.Expires = expires;
            userNameCookie.Value = user.UserName;
            passwordCookie.Value = user.Password;
            passwordCookie.HttpOnly = true;

            Response.Cookies.Add(userNameCookie);
            Response.Cookies.Add(passwordCookie);

            return new RedirectResult(String.IsNullOrEmpty(redirect) ? Url.Content("~") : redirect);
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="redirect">重定向地址</param>
        /// <returns></returns>
        public ActionResult Logout(string redirect)
        {
            DateTime expired = new DateTime(0);

            HttpCookie userNameCookie = new HttpCookie("UserName");
            HttpCookie passwordCookie = new HttpCookie("Password");
            userNameCookie.Expires = expired;
            passwordCookie.Expires = expired;

            Response.Cookies.Add(userNameCookie);
            Response.Cookies.Add(passwordCookie);

            return new RedirectResult(String.IsNullOrEmpty(redirect) ? Url.Content("~") : redirect);
        }
    }
}
